﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using kim.Common;
namespace kim.Web.dc0802.ListType
{
    public partial class AddorEdit : kim.UI.ManagePage
    {
        protected int id, IsParent;
        protected string type;
        protected void Page_Load(object sender, EventArgs e)
        {
            id = KIMRequest.GetInt("id", 0);
            IsParent = KIMRequest.GetInt("IsParent", 0);
            type = KIMRequest.GetString("type");
           
            if (!IsPostBack)
            {
                BindType();
                GetRequestValues();
            }
        }
        private void BindType()
        {
            DataSet ds = new BLL.ListTypea().GetList("IsParent=0");

            this.drp_class.Items.Add(new ListItem("显示全部分类", "0"));
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                this.drp_class.Items.Add(new ListItem(ds.Tables[0].Rows[i]["NeName"].ToString(), ds.Tables[0].Rows[i]["ListID"].ToString()));
            }
            
        }
        private void GetRequestValues()
        {

            if (type == KIMEnums.ActionEnum.Edit.ToString())
            {
                GetEditValue();
            }
            if (type == KIMEnums.ActionEnum.Delete.ToString())
            {
                bool del = new BLL.ListTypea().Delete(id);
                if (del)
                    Response.Redirect("List.aspx");
            }
            if (type != KIMEnums.ActionEnum.Edit.ToString() && type != KIMEnums.ActionEnum.Add.ToString() && type != KIMEnums.ActionEnum.Delete.ToString())
                Response.Redirect("../indexpage.aspx");
            else
            {
                //lb_po.Text = new BLL.eg_news().GetMaxSort("").ToString();
            }
        }
        private void GetEditValue()
        {
            Model.ListTypea mo = new BLL.ListTypea().GetModel(this.id);

            drp_class.SelectedValue = mo.IsParent.ToString();
            txt_Url.Text= mo.ListUrl;
            txtTitle.Text=mo.NeName;
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {

            if (type == KIMEnums.ActionEnum.Edit.ToString())
            {
                DoEdit(this.id);
                
            }
            if (type == KIMEnums.ActionEnum.Add.ToString())
            {
                DoAdd();
            }
        }

        private void DoEdit(int id)
        {
            Model.ListTypea mo = new Model.ListTypea();
            mo.ListID = id;
            if (ck_top.Checked)
                mo.IsParent = 0;
            else
                mo.IsParent = int.Parse(drp_class.SelectedValue);
            mo.ListUrl = txt_Url.Text;
            mo.NeName = txtTitle.Text;
            BLL.ListTypea bll = new BLL.ListTypea();
            if (bll.Update(mo))
            {
                lbmsg.Text = "修改成功";
                BindType();
            }
            else
                lbmsg.Text = "修改失败";

        }
        private void DoAdd()
        {
            Model.ListTypea mo = new Model.ListTypea();
           
            if (ck_top.Checked)
                mo.IsParent = 0;
            else
                mo.IsParent = int.Parse(drp_class.SelectedValue);
            mo.ListUrl = txt_Url.Text;
            mo.NeName = txtTitle.Text;
            BLL.ListTypea bll = new BLL.ListTypea();
            if (bll.Add(mo) > 0)
                lbmsg.Text = "添加成功";
            else
                lbmsg.Text = "添加失败";
        }


       
    }
}